{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n===================================================================\nSupport Vector Regression (SVR) using linear and non-linear kernels\n===================================================================\n\nToy example of 1D regression using linear, polynomial and RBF kernels.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\nimport numpy as np\nfrom sklearn.svm import SVR\nimport matplotlib.pyplot as plt\n\n# #############################################################################\n# Generate sample data\nX = np.sort(5 * np.random.rand(40, 1), axis=0)\ny = np.sin(X).ravel()\n\n# #############################################################################\n# Add noise to targets\ny[::5] += 3 * (0.5 - np.random.rand(8))\n\n# #############################################################################\n# Fit regression model\nsvr_rbf = SVR(kernel='rbf', C=100, gamma=0.1, epsilon=.1)\nsvr_lin = SVR(kernel='linear', C=100, gamma='auto')\nsvr_poly = SVR(kernel='poly', C=100, gamma='auto', degree=3, epsilon=.1,\n               coef0=1)\n\n# #############################################################################\n# Look at the results\nlw = 2\n\nsvrs = [svr_rbf, svr_lin, svr_poly]\nkernel_label = ['RBF', 'Linear', 'Polynomial']\nmodel_color = ['m', 'c', 'g']\n\nfig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10), sharey=True)\nfor ix, svr in enumerate(svrs):\n    axes[ix].plot(X, svr.fit(X, y).predict(X), color=model_color[ix], lw=lw,\n                  label='{} model'.format(kernel_label[ix]))\n    axes[ix].scatter(X[svr.support_], y[svr.support_], facecolor=\"none\",\n                     edgecolor=model_color[ix], s=50,\n                     label='{} support vectors'.format(kernel_label[ix]))\n    axes[ix].scatter(X[np.setdiff1d(np.arange(len(X)), svr.support_)],\n                     y[np.setdiff1d(np.arange(len(X)), svr.support_)],\n                     facecolor=\"none\", edgecolor=\"k\", s=50,\n                     label='other training data')\n    axes[ix].legend(loc='upper center', bbox_to_anchor=(0.5, 1.1),\n                    ncol=1, fancybox=True, shadow=True)\n\nfig.text(0.5, 0.04, 'data', ha='center', va='center')\nfig.text(0.06, 0.5, 'target', ha='center', va='center', rotation='vertical')\nfig.suptitle(\"Support Vector Regression\", fontsize=14)\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}